/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.android.accessibility.uiunderstanding;

import android.graphics.Rect;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.common.collect.ImmutableList;

/** Base interface for all snapshot related views/windows to make iteration easier. */
public interface Node {

  /** Returns a list of children of this node. */
  @NonNull
  default ImmutableList<Node> getChildNodes() {
    ImmutableList.Builder<Node> children = ImmutableList.builder();
    for (int c = 0; c < getChildNodeCount(); ++c) {
      children.add(getChildNode(c));
    }
    return children.build();
  }

  /** Returns the child at index. Returns if out of bounds. */
  @Nullable
  Node getChildNode(int index);

  /** Returns the size of children. */
  int getChildNodeCount();

  /** Returns the parent if exists. Null otherwise. */
  @Nullable
  Node getParentNode();

  /** Returns the node bounds in screen coordinates. */
  @NonNull
  default Rect getBoundsInScreen() {
    @NonNull Rect bounds = new Rect();
    getBoundsInScreen(bounds);
    return bounds;
  }

  /** Modifies Rect to hold the node bounds in screen coordinates. */
  default void getBoundsInScreen(@NonNull Rect outBounds) {}
}
